import sqlite3

def connect_to_database():
    try:
        # 连接到 SQLite 数据库
        # 数据库文件是 example.db
        # 如果数据库不存在，它将被创建
        conn = sqlite3.connect('example.db')

        # 创建一个游标对象
        cursor = conn.cursor()

        # 创建表
        cursor.execute('''
            CREATE TABLE IF NOT EXISTS users (
                id INTEGER PRIMARY KEY,
                name TEXT NOT NULL,
                phone TEXT NOT NULL
            )
        ''')

        # 插入数据
        cursor.execute("INSERT INTO users (name, phone) VALUES (?, ?)", ('张三', '1234567890'))

        # 提交事务
        conn.commit()

        # 查询数据
        cursor.execute("SELECT * FROM users WHERE name = ?", ('张三',))
        row = cursor.fetchone()
        print(row)

    except sqlite3.Error as e:
        print(f"数据库错误: {e}")
    finally:
        # 关闭连接
        if conn:
            conn.close()

# 调用函数
connect_to_database()
